<?php
namespace App\Services;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\Storage;
use EasyWeChat\Factory;
use Illuminate\Support\Facades\Redis;
use Log,Exception,Config;
use Symfony\Component\Cache\Adapter\RedisAdapter;

class MiniapptokenService
{
    // 多悦微服开放授权
    public static function openPlatform($componentAppid="")
    {
        if (empty($componentAppid)) $componentAppid = "wxa755405206410fd2";
        $config = Config::get("wechats.component_platform.".$componentAppid);
        $component_verify_ticket = Redis::get("easywechat.open_platform.verify_ticket.wxa755405206410fd2");
        dd($component_verify_ticket);
        $client = new Client;
        $params = ["component_appid"=>$config["app_id"],"component_appsecret"=>$config["secret"],"component_verify_ticket"=>$config["secret"]];
        $response = $client->request('POST', 'https://api.weixin.qq.com/cgi-bin/component/api_component_token', [
            'body' => json_encode($params,JSON_UNESCAPED_UNICODE),
            'headers' => [
                'Content-type'=> 'application/json',
                'Accept'=>'application/json'
            ]
        ]);
        $body = $response->getBody();
        $jsonbody = json_decode($body,true);
        dd($jsonbody);
        return $jsonbody;
    }

    // 获取开发component_access_token
    public static function getComponentAccessToken($componentAppid)
    {
        $openPlatform = self::openPlatform($componentAppid);
        $accessToken = $openPlatform->access_token;
        $token = $accessToken->getToken();
        $component_access_token = $token["component_access_token"];
        return $component_access_token;
    }

    // 获取代小程序access_token
    public static function getMiniProgramAccessToken($componentAppid,$appId,$refreshToken)
    {
        $openPlatform = self::openPlatform($componentAppid);
        $miniProgram = $openPlatform->miniProgram($appId,$refreshToken);
        $accessToken = $miniProgram->access_token;
        $token = $accessToken->getToken();
        $authorizer_access_token = $token["authorizer_access_token"];
        return $authorizer_access_token;
    }

    // 刷新access_token
    public static function getMiniProgramAccessTokenForce($componentAppid,$appId,$refreshToken)
    {
        $openPlatform = self::openPlatform($componentAppid);
        $miniProgram = $openPlatform->miniProgram($appId,$refreshToken);
        $accessToken = $miniProgram->access_token;
        $token = $accessToken->getToken(true);
        $authorizer_access_token = $token["authorizer_access_token"];
        return $authorizer_access_token;
    }

    // 获取代小程序access_token(新)
    public static function getMiniProgramAccessTokenNew($componentAppid,$appId,$refreshToken)
    {
        $openPlatform = self::openPlatform($componentAppid);
        $miniProgram = $openPlatform->miniProgram($appId,$refreshToken);
        $accessToken = $miniProgram->access_token;
        $token = $accessToken->getToken();
        $authorizer_access_token = $token["authorizer_access_token"];
        return $authorizer_access_token;
    }
}